package com.ruoyi.operation.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.operation.domain.bo.THKDateBo;
import com.ruoyi.system.api.RemoteSysConfigService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.operation.domain.vo.TExchangerateVo;
import com.ruoyi.operation.domain.bo.TExchangerateBo;
import com.ruoyi.operation.service.ITExchangerateService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 来自finchina的汇率控制器
 * 前端访问路由地址为:/operation/exchangerate
 *
 * @author ruoyi
 * @date 2025-09-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/exchangerate")
public class TExchangerateController extends BaseController {

    private final ITExchangerateService iTExchangerateService;

    private static final String IMPORT_PATH = "operation:exchange:rate";
    @DubboReference
    private RemoteSysConfigService remoteSysConfigService;

    /**
     * 查询来自finchina的汇率列表
     */
    @GetMapping("/list")
    public TableDataInfo<TExchangerateVo> list(TExchangerateBo bo, PageQuery pageQuery) {
        return iTExchangerateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出来自finchina的汇率列表
     */
    @Log(title = "来自finchina的汇率", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TExchangerateBo bo, HttpServletResponse response) {
        List<TExchangerateVo> list = iTExchangerateService.queryList(bo);
        ExcelUtil.exportExcel(list, "来自finchina的汇率", TExchangerateVo.class, response);
    }

    /**
     * 获取来自finchina的汇率详细信息
     *
     * @param natureday 主键
     */
    @GetMapping("/{natureday}")
    public R<TExchangerateVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String natureday) {
        return R.ok(iTExchangerateService.queryById(natureday));
    }

    /**
     * 新增来自finchina的汇率
     */
    @Log(title = "来自finchina的汇率", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TExchangerateBo bo) {
        return toAjax(iTExchangerateService.insertByBo(bo));
    }

    /**
     * 修改来自finchina的汇率
     */
    @Log(title = "来自finchina的汇率", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TExchangerateBo bo) {
        return toAjax(iTExchangerateService.updateByBo(bo));
    }

    /**
     * 删除来自finchina的汇率
     *
     * @param naturedays 主键串
     */
    @Log(title = "来自finchina的汇率", businessType = BusinessType.DELETE)
    @DeleteMapping("/{naturedays}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] naturedays) {
        return toAjax(iTExchangerateService.deleteWithValidByIds(Arrays.asList(naturedays), true));
    }



    @PostMapping("/import")
    @Log(title = "倒入当日汇率",businessType = BusinessType.INSERT)
    public R<String> importData(@RequestBody THKDateBo thkDateBo){
        String path = remoteSysConfigService.selectConfigByKey(IMPORT_PATH);
        if (path.contains("yyyyMMdd")){
            path = path.replace("yyyyMMdd",thkDateBo.getStepDate());
        }
        thkDateBo.setBasePath(path);
//        String filePath = path+"LSFA_EXCHANGERATE_"+thkDateBo.getStepDate()+".CSV";
        //1、倒入汇率文件
        return iTExchangerateService.executeStepDate(thkDateBo);

    }

}
